package com.controller;

import java.io.IOException;

import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import com.model.Guess;
import com.service.UserService;
import java.util.List;


public class GuessController extends HttpServlet {
   

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            UserService userService = new UserService();

            userService.setConnection();
            List<Guess> list = userService.getAllGuess();

            userService.closeConnection();

            request.setAttribute("viewguess", list);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewguess.jsp");
            requestDispatcher.forward(request, response);

        } catch (Exception ex) {

            ex.printStackTrace();

        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            String guessCategory = request.getParameter("guessCategory");
            String guessContent = request.getParameter("guessContent");
            String guessCreatedBy = request.getParameter("userName");
            String guessExpiresOn = request.getParameter("guessExpiresOn");


            boolean error = false;

            if (guessCategory.length() == 0 || guessCategory == null) {
                error = true;
            }
            if (guessContent.length() == 0 || guessContent == null) {
                error = true;
            }
            if (guessCreatedBy.length() == 0 || guessCreatedBy == null) {
                error = true;
            }

            if (error) {
                request.setAttribute("errorMsg", "All Fields are required !");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/guess.jsp");
                requestDispatcher.forward(request, response);
            } else {
                request.setAttribute("successMsg", "Congrats! You have created a guess Successfully");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/guess.jsp");
                requestDispatcher.forward(request, response);


                Guess guess = new Guess();

                guess.setGuessCategory(guessCategory);
                guess.setGuessContent(guessContent);
                guess.setGuessCreatedBy(guessCreatedBy);
                guess.setGuessExpiryDate(guessExpiresOn);

                guess.setCurrentDateTime();
                guess.setSpotOnCount(0);
                guess.setDeadWrongCount(0);
                

                UserService userService = new UserService();

                userService.setConnection();
                userService.createGuess(guess);
                userService.closeConnection();


            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();

        } catch (SQLException ex) {
            ex.printStackTrace();

        }

    }
}
